package com.sample.andremion.musicplayer.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.widget.ImageView;
import android.widget.TextView;

import com.sample.andremion.musicplayer.R;
import com.sample.andremion.musicplayer.music.MusicContent.MusicItem;
import com.sample.andremion.musicplayer.music.MusicControl;
import com.sample.andremion.musicplayer.utils.MediaUtil;

import java.util.List;

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
    private final List<MusicItem> mValues;
    private Context mContext;
    private Bitmap smallImage;

    public RecyclerViewAdapter(List<MusicItem> items, Context context) {
        mValues = items;
        mContext = context;
        smallImage = MediaUtil.getDefaultArtwork(context, true);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.content_list_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        holder.mItem = mValues.get(position);
        holder.mTitleView.setText(holder.mItem.mTitle);
        holder.mArtistView.setText(holder.mItem.mArtist);
        holder.mDurationView.setText(DateUtils.formatElapsedTime(holder.mItem.mDuration));

        if (holder.mCoverView.getTag() == null)
            holder.mCoverView.setTag(true);

        if (AsynLoadRecycleView.isScroll) {
            holder.mCoverView.setImageBitmap(smallImage);
            holder.mCoverView.setTag(true);
        }
        else {
            holder.mCoverView.setImageBitmap(MediaUtil.getArtwork(mContext, holder.mItem.mId, holder.mItem.mAlbumId, true, true));
            if ((Boolean) holder.mCoverView.getTag()) {
                holder.mCoverView.setTag(false);
                holder.mCoverView.setAlpha(0.4f);
                holder.mCoverView.animate().alpha(1f).setInterpolator(new AccelerateInterpolator()).start();
            }
        }

        holder.mView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (position == MusicControl.getPosition()) {
                    MusicControl.playStop();
                    if (listener != null)
                        listener.playStop();
                }else {
                    MusicControl.play(position);
                }
            }
        });

        if (position == MusicControl.getPosition())
            holder.mPlayStatus.setVisibility(View.VISIBLE);
        else
            holder.mPlayStatus.setVisibility(View.INVISIBLE);

        holder.mView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if(listener != null){
                    if (position == MusicControl.getPosition())
                        MusicControl.play(position);
                    notifyDataSetChanged();
                    listener.onLongClickListener();
                    return true;
                }
                return false;
            }
        });
    }

    @Override
    public int getItemCount() {
        return mValues.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public final View mView;
        public final ImageView mCoverView;
        public final TextView mTitleView;
        public final TextView mArtistView;
        public final TextView mDurationView;
        protected final ImageView mPlayStatus;
        public MusicItem mItem;

        public ViewHolder(View view) {
            super(view);
            mView = view;
            mPlayStatus = (ImageView) view.findViewById(R.id.playStatus);
            mCoverView = (ImageView) view.findViewById(R.id.cover);
            mTitleView = (TextView) view.findViewById(R.id.title);
            mArtistView = (TextView) view.findViewById(R.id.artist);
            mDurationView = (TextView) view.findViewById(R.id.duration);
        }
    }

    OnItemClickListener listener;

    public void setOnItemClickListener(OnItemClickListener l){
        listener = l;
    }

    public void removeOnItemClickListener() {
        listener = null;
    }

    public interface OnItemClickListener {
        void onLongClickListener();
        void playStop();
    }
}
